/**
 * 
 */
package com.framework.util.datasource;

import java.io.BufferedReader;
import java.io.BufferedWriter;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.FileReader;
import java.io.FileWriter;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.io.OutputStreamWriter;
import java.net.URL;


/** 
* @author 李品良: 
* @version 创建时间：2016年7月18日 上午10:28:22 
* 类说明 
*/
/**
 * @author Administrator
 *
 */
public class RunsqlUtils {

	
	/**
	 * 
	* @author 李品良: 
	* @version 创建时间：2016年7月18日 下午2:11:21 
	* @param dbname
	* @param dbusername
	* @param dbpassword
	* @param ipaddress
	* @param filepath  文件保存到哪里去
	* @param fileNamebak 文件从source文件里面读取  ，例子"/con1000.sql"
	* @return
	 */
	public String alterSql(String dbname, String dbusername, String dbpassword,String filepath,String fileNamebak,boolean flag) {
		URL pathbak = getClass().getResource(fileNamebak);
		String filepathbak;
		if (flag) {
			filepathbak = pathbak.getPath().substring(1);
		}else{
			filepathbak = pathbak.getPath();
		}
		StringBuffer sb=null;

		// 读取文件内容
		sb = readFileByLines(filepathbak);
		
		
		// 开始是修改sql文件
		int first = sb.indexOf("db22222");
		int size = 7;
		StringBuffer stringBuffer = sb.replace(first, first + size, dbname);
		int first2 = sb.lastIndexOf("db22222");
		StringBuffer stringBuffer2 = stringBuffer.replace(first2, first2 + size, dbname);

		int intdbusername = sb.indexOf("dbusername");
		int dbunsize = "dbusername".length();
		stringBuffer2 = stringBuffer2.replace(intdbusername, intdbusername + dbunsize, dbusername);

		int intdbpassword = stringBuffer2.indexOf("dbpassword");
		int dbpwsize = "dbpassword".length();
		stringBuffer2 = stringBuffer2.replace(intdbpassword, intdbpassword + dbpwsize, dbpassword);

		
		filepath = writeSql(stringBuffer2.toString(), filepath);
		// TODO
		System.out.println("输出完成      ==="+filepath);
		return filepath;
		
	}

	public StringBuffer ridofbugs(StringBuffer sb) {
		sb.delete(0, 5);
		return sb;
	}
	
	
	/**
	 * 以字符为单位读取文件，常用于读文本，数字等类型的文件
	 */
	/**
	 * 以行为单位读取文件，常用于读面向行的格式化文件
	 */
	public StringBuffer readFileByLines(String fileName) {
		File file = new File(fileName);
		BufferedReader reader = null;
		
		StringBuffer sb = new StringBuffer();
		try {
			FileInputStream fileInputStream=new FileInputStream(file);
			InputStreamReader inputStreamReader=new InputStreamReader(fileInputStream, "UTF-8");
			reader = new BufferedReader(inputStreamReader);
			String tempString = null;
			int line = 1;
			// 一次读入一行，直到读入null为文件结束
			while ((tempString = reader.readLine()) != null) {
				// 显示行号
				sb.append(tempString + "\r\n");
				line++;
			}
			reader.close();
		} catch (IOException e) {
			e.printStackTrace();
		} finally {
			if (reader != null) {
				try {
					reader.close();
				} catch (IOException e1) {
				}
			}
		}

		return sb;
	}

	/**
	 * 
	* @author 李品良: 
	* @version 创建时间：2016年7月18日 上午11:34:02 
	* @param content 输入的sql文本内容
	* @param filepath 是输出的位置
	* @return
	 */
	public String writeSql(String content, String filepath) {

		System.out.println("filepath=" + filepath);

		try {
			// true = append file
			
			File file=new File(filepath);
			System.out.println("File is created!");  
	        //Runtime.getRuntime().exec("chmod 777 /home/test3.txt");   
			if (!file.exists()) {
				file.createNewFile();
			}
	        file.setExecutable(true);//设置可执行权限  
	        file.setReadable(true);//设置可读权限  
	        file.setWritable(true);//设置可写权限  
			System.out.println("File is changed!");  
			
			FileOutputStream fileOutputStream=new FileOutputStream(file);
			OutputStreamWriter outputStreamWriter=new OutputStreamWriter(fileOutputStream,"UTF-8");
			BufferedWriter bufferWritter = new BufferedWriter(outputStreamWriter);
			bufferWritter.write(content);
			
			bufferWritter.close();
			outputStreamWriter.close();
			fileOutputStream.close();

		} catch (Exception e) {
			e.printStackTrace();
			System.out.println("File set permission failed!");  
			return null;
		}
		return filepath;
	}
	
}
